package com.dzj.fishingcommunity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzj.fishingcommunity.common.ResponseResult;
import com.dzj.fishingcommunity.model.LikeTable;
import com.dzj.fishingcommunity.service.LikeRecordService;
import com.dzj.fishingcommunity.service.LikeTableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/like-tables")
@Tag(name = "点赞管理", description = "点赞管理相关的接口")
public class LikeTableController {

    @Autowired
    private LikeTableService likeTableService;
    @Autowired
    private LikeRecordService likeRecordService;

    @Operation(summary = "获取所有点赞记录")
    @GetMapping
    public ResponseResult<List<LikeTable>> getAllLikeTables(
            @Parameter(description = "当前页码", required = false) @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") int size) {
        Page<LikeTable> page = new Page<>(current, size);
        Page<LikeTable> likeTablePage = likeTableService.page(page);
        return ResponseResult.success(likeTablePage.getRecords());
    }

    @Operation(summary = "根据ID获取点赞记录")
    @GetMapping("/{likeId}")
    public ResponseResult<LikeTable> getLikeTableById(@PathVariable Integer likeId) {
        LikeTable likeTable = likeTableService.getById(likeId);
        return ResponseResult.success(likeTable);
    }

    @Operation(summary = "新增点赞记录")
    @PostMapping
    public ResponseResult<LikeTable> addLikeTable(@RequestBody LikeTable likeTable) {
        likeTable.setLikeTime(new java.util.Date());
        //1.根据帖子id,用户id查询点赞记录是否存在
        LikeTable like = likeTableService.lambdaQuery()
                .eq(LikeTable::getPostId, likeTable.getPostId())
                .eq(LikeTable::getUserId, likeTable.getUserId())
                .one();
        if(like != null) {
            //证明点过赞，所以此操作是取消点赞
            likeTableService.remove(new QueryWrapper<LikeTable>().eq("post_id", likeTable.getPostId()).eq("user_id", likeTable.getUserId()));

            //需要将点赞记录表中的点赞数量减1，而且将点赞记录表的取消点赞数+1
            Boolean isCancel = likeRecordService.cancelLike(likeTable.getPostId());
            if (!isCancel){
                return ResponseResult.error(400, "取消点赞出现异常");
            }
        }

        //2.将点赞同步到点赞记录表
        Boolean isAdded = likeRecordService.addCountByPostIdAndUserId(likeTable.getPostId());
        if (!isAdded) {
            return ResponseResult.error(400, "点赞记录新增出现异常");
        }
        likeTableService.save(likeTable);
        return ResponseResult.success(likeTable);
    }

    @Operation(summary = "更新点赞记录")
    @PutMapping("/{likeId}")
    public ResponseResult<LikeTable> updateLikeTable(@PathVariable Integer likeId, @RequestBody LikeTable likeTable) {
        likeTable.setLikeId(likeId);
        likeTableService.updateById(likeTable);
        return ResponseResult.success(likeTable);
    }

    @Operation(summary = "删除点赞记录")
    @DeleteMapping("/{likeId}")
    public ResponseResult<Void> deleteLikeTable(@PathVariable Integer likeId) {
        boolean isDeleted = likeTableService.removeById(likeId);
        if (!isDeleted) {
            return ResponseResult.error(400, "点赞记录删除出现异常");
        }
        return ResponseResult.success(null);
    }
}